import com.aliyun.oss.OSSClient;
import org.jsoup.Jsoup;

import java.io.ByteArrayInputStream;
import java.sql.*;
import java.util.*;

public class UploadImageToLocal {
    // 上传到oos
    private static OSSClient oss = OSSConfig.getImageOSSClient();
    // 上传到oos路径
    private static String path = "Goods/source/";

    public static void main(String[] args) {
        Map<String, String> imgUrl = getImgUrl();
        System.out.println(imgUrl.size());
        upload(imgUrl);
    }

    private static void upload(Map<String, String> imgUrl) {
        int count = 0;
        if (null != imgUrl && imgUrl.size() > 0) {
            List<String> exceptionUrl = new ArrayList<String>();
            Iterator<Map.Entry<String, String>> iterator = imgUrl.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> next = iterator.next();
                String image_tmbq = next.getKey();
                if (image_tmbq.contains("@!@")) {
                    image_tmbq = image_tmbq.split("@!@")[0];
                }
                String image_bevol = next.getValue();
                try {
                    // 有的链接后面还带有参数 吧参数去掉
                    org.jsoup.Connection connect = Jsoup.connect(image_tmbq);
                    connect.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36");
                    org.jsoup.Connection.Response resultImageResponse = connect.ignoreContentType(true).execute();
                    byte[] bodyAsBytes = resultImageResponse.bodyAsBytes();
                    // 路径规则是oos文件夹路径+随机uuid—+文件后缀
                    String finalPath = path + image_bevol;
                    System.out.println(finalPath);
                    oss.putObject("pro-static-file", finalPath, new ByteArrayInputStream(bodyAsBytes));
                    count++;
                } catch (Exception e) {
                    exceptionUrl.add(image_tmbq);
                    System.out.println(e.getMessage());
                }
            }

            for (String e :exceptionUrl){
                System.out.println("exception url----"+e);
            }
        }
        System.out.println("总计下载推送：" + count + "--张图片");
    }

    private static Map<String, String> getImgUrl() {
        Map<String, String> urlMap = new HashMap<>();
        //下载链接和保存链接在mysql
        Connection conn;
        Statement statement;
        String driver = "com.mysql.jdbc.Driver";
        String dburl = "jdbc:mysql://rm-bp1cr0x9v71z7boquso.mysql.rds.aliyuncs.com:3306/app_test";
        String user = "app_test_rw";
        String password = "52Rh&4m&$J*D";
        String sql = "SELECT * FROM `temp_aaaaaa_tmbq_image_down` WHERE  image_tmbq != \"\"";

        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(dburl, user, password);
            statement = conn.createStatement();//实例化Statement对象，用stmt接收
            ResultSet rs = statement.executeQuery(sql);
            //执行sql
            while (rs.next()) {
                String image_tmbq = rs.getString("image_tmbq");
                String image_bevol = rs.getString("image_bevol");
                //将成分链接连接到队列
                urlMap.put(image_tmbq, image_bevol);
            }
            //关闭链接
            rs.close();
            statement.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }

        return urlMap;
    }

}
